﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
using Company.Web.Areas.Manage.Models.ImagesModels;
using Company.Web.Commen;

namespace Company.Web.Areas.Manage.Controllers
{
    public class ImagesController : ControllerBase
    {
        string[] types = { ".jpg", ".gif", ".png", "ico", "bmp" };
        public ActionResult Index(IndexModel model)
        {
            if (model == null)
                model = new IndexModel();
            if (model.A < 1)
                model.A = null;
            if (model.B < 1)
                model.B = null;

            var page = model.A ?? 1;
            var size = model.B ?? 18;

            var path = ConfigurationManager.AppSettings["ImgUpLoadPath"] ?? "~/Content/Products/";

            model.ServerPath = path;
            path = Server.MapPath(path);

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            model.Root = new DirectoryInfo(path);
            model.Current = new DirectoryInfo(Path.Combine(path, model.ID ?? string.Empty));

            if (!model.Current.Exists)
                return RedirectToAction("index", new { id = string.Empty, model.A, model.B });

            var fs = model.Current.GetFiles().Where(p => types.Contains((p.Extension ?? "").ToLower())).OrderBy(p => p.Name);
            model.SumCount = fs.Count();
            model.Items = fs.Skip(size * (page - 1)).Take(size).ToArray();

            return View("Index", model);
        }

        [ActionName("Index")]
        [MultiButton("BtnDel")]
        public ActionResult Delete(IndexModel model, string[] name)
        {
            if (model == null)
                model = new IndexModel();
            if (name == null || name.Length == 0)
                ViewData["msg"] = "请选择要删除的图片".AlertOffPage();
            else
            {
                var path = ConfigurationManager.AppSettings["ImgUpLoadPath"] ?? "~/Content/Products/";

                string path1 = Path.Combine(Server.MapPath(path), model.ID);
                if (!Directory.Exists(path1))
                    return Index(model);

                foreach (var item in name)
                {
                    try { System.IO.File.Delete(Path.Combine(path1, item)); }
                    catch { }
                }

                //ViewData["msg"] = "操作成功".AlertOffPage();
                return Content(Url.Action("index", new { model.ID, model.A, model.B }).Redirect("操作成功"));
            }

            return Index(model);
        }

        [ActionName("Index")]
        [MultiButton("UpFile")]
        public ActionResult UpFile(IndexModel model)
        {
            if (model == null)
                model = new IndexModel();
            if (Request.Files.Count == 0)
                return Content(Url.Action("index", new { model.ID, model.A, model.B }).Redirect("请选择要上传的图片"));
            else
            {
                var path = ConfigurationManager.AppSettings["ImgUpLoadPath"] ?? "~/Content/Products/";

                model.ServerPath = path;
                string path1 = Path.Combine(Server.MapPath(path), model.ID);
                if (!Directory.Exists(path1))
                    Directory.CreateDirectory(path1);

                foreach (var key in Request.Files.AllKeys)
                {
                    var item = Request.Files[key];
                    if (item.InputStream.Length == 0)
                        continue;
                    var sm = item.InputStream;
                    var name = item.FileName;
                    if (item.FileName.IndexOf('\\') >= 0)
                        name = name.Substring(name.LastIndexOf('\\') + 1);

                    var tp = Path.Combine(path1, string.Format("{0:yyyyMMddHHmmss}_{1}", DateTime.Now, name));
                    var f = System.IO.File.Create(tp);

                    int l = 0;
                    var bf = new byte[2048];
                    while ((l = sm.Read(bf, 0, bf.Length)) > 0)
                        f.Write(bf, 0, l);
                    f.Close();
                }

                return Content(Url.Action("index", new { model.ID, model.A, model.B }).Redirect("上传成功"));

            }
        }
    }
}
